﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CloudSpawn : MonoBehaviour
{
    //Position
    //Player
    //LastPosition

    public float minX, maxX;
    public float distanceY;
    public float lastCloudY;

    public GameObject cloud;

    public GameObject player;

    public List<GameObject> clouds;

    public List<GameObject> collectors;

    public int controlCloud = 0;
    // Use this for initialization
    void Start()
    {
        Shuffle();
        CreateClouds();  
        PlayerPosition();
        for (var i = 0; i < collectors.Count; i++)
        {
            collectors [i].SetActive(false);
        }
    }

    void Shuffle()
    {
        for (var i = 0; i < clouds.Count; i++)
        {
            var target = Random.Range(i, clouds.Count);
            var temp = clouds [i];
            clouds [i] = clouds [target];
            clouds [target] = temp;
        }
    }


    void CreateClouds()
    {

        var worldSize = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0));
        var worldSize2 = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0));
        Debug.Log("WorldSize: " + worldSize);
        Debug.Log("WorldSize2: " + worldSize2);
        minX = worldSize2.x + 0.5f;
        maxX = worldSize.x - 0.5f;


        lastCloudY = 0;
        for (var i = 0; i < clouds.Count; i++)
        {
            var cloud = clouds [i];
            Vector3 pos;
            float x = 0;
            if (controlCloud == 0)
            {
                x = Random.Range(0, maxX);
                controlCloud = 1;
            } else if (controlCloud == 1)
            {
                x = Random.Range(minX, 0);
                controlCloud = 2;
            } else if (controlCloud == 2)
            {
                x = Random.Range(1, maxX);
                controlCloud = 3;
            } else if (controlCloud == 3)
            {
                x = Random.Range(minX, -1);
                controlCloud = 0;
            }
            pos = new Vector3(x, lastCloudY, 0);
            cloud.transform.localPosition = pos;

            lastCloudY -= distanceY;
        }

    }

    void PlayerPosition()
    {
        if (clouds [0].tag == "darkCloud")
        {
            var c0 = clouds [0];
            var temp = c0.transform.localPosition;
            GameObject baiCloud = null;
            int baiId = 0;
            for (var i = 1; i < clouds.Count; i++)
            {
                if (clouds [i].tag == "cloud")
                {
                    baiCloud = clouds [i];
                    baiId = i;
                    break;
                }
            }

            c0.transform.localPosition = baiCloud.transform.localPosition;
            baiCloud.transform.localPosition = temp;

            clouds [0] = baiCloud;
            clouds [baiId] = c0;
        }

        var c = clouds [0].transform.localPosition;
        player.transform.localPosition = c + new Vector3(0, 2, 0);

    }

    void OnTriggerEnter2D(Collider2D target)
    {
        if (target.tag == "cloud" || target.tag == "darkCloud")
        {
            if (Mathf.Abs(target.transform.localPosition.y - (lastCloudY + distanceY)) < 0.1f)
            {
                Shuffle();
                for (var i = 0; i < clouds.Count; i++)
                {
                    var cloud = clouds [i];
                    if (cloud.activeSelf == false)
                    {
                        Vector3 pos;
                        float x = 0;
                        if (controlCloud == 0)
                        {
                            x = Random.Range(0, maxX);
                            controlCloud = 1;
                        } else if (controlCloud == 1)
                        {
                            x = Random.Range(minX, 0);
                            controlCloud = 2;
                        } else if (controlCloud == 2)
                        {
                            x = Random.Range(1, maxX);
                            controlCloud = 3;
                        } else if (controlCloud == 3)
                        {
                            x = Random.Range(minX, -1);
                            controlCloud = 0;
                        }
                        pos = new Vector3(x, lastCloudY, 0);
                        cloud.transform.localPosition = pos;
                        cloud.SetActive(true);
                        lastCloudY -= distanceY;


                        var random = Random.Range(0, collectors.Count);
                        var col = collectors [random];
                        if (col.activeSelf == false)
                        {
                            if(cloud.tag == "cloud") {
                                var temp = cloud.transform.localPosition;
                                temp.y += 0.5f;
                                col.transform.localPosition = temp;
                                col.SetActive(true);
                            }
                        }
                    }
                }
            }
        }
    }
}
